package com.amoyt.project.controller.admin;


import com.amoyt.project.annotations.BusinessLog;
import com.amoyt.project.annotations.ModuleLog;
import com.amoyt.project.domain.dto.insert.HouseholdAddDto;
import com.amoyt.project.domain.dto.query.PovertyHouseholdPageQuery;
import com.amoyt.project.domain.dto.update.HouseholdUpdateDto;
import com.amoyt.project.domain.res.Result;
import com.amoyt.project.exception.BusinessException;
import com.amoyt.project.exception.enums.BusinessExceptionEnum;
import com.amoyt.project.service.admin.PovertyHouseholdBaseService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 贫困户基本信息表（家庭级） 前端控制器
 * </p>
 *
 * @author AmoyT
 * @since 2025-08-21
 */
@Slf4j
@RestController
@RequestMapping("/admin/household")
@Tag(name = "贫困户信息接口",description = "贫困户信息接口")
@ModuleLog(module = "贫困户信息模块")
public class HouseholdController {

    @Autowired
    private PovertyHouseholdBaseService householdBaseService;

    /**
     * 贫困户信息列表
     * @param query 查询参数
     * @return
     */
    @PostMapping("/list")
    @Operation(summary = "贫困户信息列表",method = "POST")
    @Parameter(name = "token",description = "访问令牌",required = true,in = ParameterIn.HEADER)
    @PreAuthorize("hasAnyRole('super_admin','prov_admin','city_admin','region_admin')")
    public Result list(@RequestBody PovertyHouseholdPageQuery query){
        return householdBaseService.list(query);
    }

    /**
     * 查看单个贫困户信息详情
     * @param householdId
     * @return
     */
    @GetMapping("/detail/{householdId}")
    @Operation(summary = "查看单个贫困户信息详情",method = "GET")
    @Parameter(name = "token",description = "访问令牌",required = true,in = ParameterIn.HEADER)
    @PreAuthorize("hasAnyRole('super_admin','prov_admin','city_admin','region_admin')")
    public Result queryHouseholdDetail(@PathVariable String householdId){
        if(householdId == null || householdId.equals("")){
            throw new BusinessException(BusinessExceptionEnum.PARAM_ERROR);
        }
        return householdBaseService.queryDetail(householdId);
    }


    /**
     * 添加贫困户信息
     * @param householdAddDto
     * @return
     */
    @PostMapping("/add/{flag}")
    @Operation(summary = "添加贫困户信息",method = "POST")
    @Parameter(name = "token",description = "访问令牌",required = true,in = ParameterIn.HEADER)
    @PreAuthorize("hasAnyRole('super_admin','prov_admin','city_admin','region_admin')")
    @BusinessLog(business = "添加贫困户信息")
    public Result add(@PathVariable Integer flag, @RequestBody HouseholdAddDto householdAddDto){
        log.info("添加贫困户信息, flag={}, dto={}",flag,householdAddDto);
        //基础校验参数
        if(householdAddDto ==null || flag == null){
            throw new BusinessException(BusinessExceptionEnum.PARAM_ERROR);
        }
        return householdBaseService.add(flag,householdAddDto);
    }

    /**
     * 删除贫困户信息
     * @param householdIds
     * @return
     */
    @DeleteMapping("delete/{householdIds}")
    @Operation(summary = "删除贫困户信息",method = "DELETE")
    @Parameter(name = "token",description = "访问令牌",required = true,in = ParameterIn.HEADER)
    @PreAuthorize("hasAnyRole('super_admin')")
    @BusinessLog(business = "删除贫困户信息")
    public Result delete(@PathVariable List<String> householdIds){
        return householdBaseService.delete(householdIds);
    }

    /**
     * 更新贫困户信息
     * @param householdUpdateDto
     * @return
     */
    @PutMapping("update")
    @Operation(summary = "更新贫困户基本信息",method = "PUT")
    @Parameter(name = "token",description = "访问令牌",required = true,in = ParameterIn.HEADER)
    @PreAuthorize("hasAnyRole('super_admin','prov_admin','city_admin','region_admin')")
    @BusinessLog(business = "更新贫困户信息")
    public Result update(@RequestBody HouseholdUpdateDto householdUpdateDto){
        if(householdUpdateDto == null || householdUpdateDto.getHouseholdId() == null){
            throw new BusinessException(BusinessExceptionEnum.PARAM_ERROR);
        }
        return householdBaseService.update(householdUpdateDto);
    }

}

